package com.simple.datax.dao.impl;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.datax.dao.IDataxSendChannelDAO;
import com.simple.datax.vo.DataxSendChannelVO;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;

@Repository("dataxSendChannelDAO")
public class DataxSendChannelDAOImpl extends BaseDAO implements
		IDataxSendChannelDAO {

	@Override
	public DataxSendChannelVO findById(Integer fiId) {
		return (DataxSendChannelVO)super.get(DataxSendChannelVO.class, fiId);
	}

	@Override
	public void saveOrUpdate(DataxSendChannelVO dataxSendChannelVO) {
		super.saveOrUpdate(dataxSendChannelVO);
	}

	@Override
	public List<DataxSendChannelVO> findAll() {
		return super.query(DataxSendChannelVO.class,Order.asc("fiId"));
	}

	@Override
	public void delete(DataxSendChannelVO dataxSendChannelVO) {
		super.delete(dataxSendChannelVO);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DataxSendChannelVO> findDataxSendChannelVOByPage(
			PageInfoBean pageInfoBean, String fiId, String fcCommunType,
			String fcMsgFormateId, String fcMsgType) {
		final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
        final StringBuffer sb = new StringBuffer(500);
        
        sb.append( "select  d from DataxSendChannelVO d where 1>0 " );
        if(!StringUtil.isNull(fiId)){
        	sb.append( " and d.fiId =:fiId ");
        	 paramMap.put("fiId", Integer.parseInt(fiId));
        }
        if(!StringUtil.isNull(fcCommunType)){
            sb.append( " and d.fcCommunType=:fcCommunType" );
            paramMap.put("fcCommunType", fcCommunType);
        }
        if(!StringUtil.isNull(fcMsgFormateId)){
            sb.append( " and d.fcMsgFormateId=:fcMsgFormateId" );
            paramMap.put("fcMsgFormateId", fcMsgFormateId);
        }
        if(!StringUtil.isNull(fcMsgType)){
            sb.append( " and d.fcMsgType=:fcMsgType" );
            paramMap.put("fcMsgType", fcMsgType);
        }
        sb.append( " order by d.fiId desc" );
        
		return (List<DataxSendChannelVO>)this.queryListByPage("d.fiId", sb.toString(), paramMap, pageInfoBean);
	}

	@Override
	public Integer generateId() {
		List<?> list =  getSession().createSQLQuery("select SEQ_SEND_CHANNEL_ID.nextval from dual").list();
		return ((BigDecimal)(list.get(0))).intValue();
	}

	@Override
	public List<DataxSendChannelVO> getDataxSendChannelVOByPage(
			PageInfoBean pageInfoBean, String fcChannelDesc) {
        final StringBuffer sb = new StringBuffer(500);
		sb.append( "select  d from DataxSendChannelVO d where  " );
	    sb.append( " d.fcChannelDesc like ? " );
		sb.append( " order by d.fiId desc" );
		return super.query(sb.toString(), "%"+fcChannelDesc+"%");
	}

}
